package testing;

import java.util.Hashtable;
import java.util.Map;

import dse.Cluster;
import dse.DataObject;
import dse.DataObjectAccessRanking;
import dse.DseException;
import dse.ExperimentConfiguration;
import dse.Network;
import dse.Node;

public class MultipleRequestScheduler 
extends dse.modules.GeneralRequestEventScheduler
{
	private Map<Node,DataObject> requestsToMake;
	private int reqCount;
	
	
	@Override
	public void initalize (
			ExperimentConfiguration systemSettings, 
			Map<String, String> moduleSettings
			)
	throws DseException {
		super.initalize(systemSettings, moduleSettings);
		
		reqCount = Integer.parseInt(moduleSettings.get("requests-to-make"));
	}




	public void createRequests (
			Network network,
			Map<Cluster, DataObjectAccessRanking> accessRankings
			)
	{
		requestsToMake = new Hashtable<Node, DataObject>();
		while (requestsToMake.size() < reqCount) {
			Map<Node, DataObject> m = 
				super.getRequests(network, accessRankings);
			for (Node n : m.keySet()) {
				requestsToMake.put(n, m.get(n));
				if (requestsToMake.size() >= reqCount)
					break;
			}
		}		
	}
	
	
	public Map<Node, DataObject> getRequests (
			Network network,
			Map<Cluster, DataObjectAccessRanking> accessRankings
			)
	{
		if (requestsToMake == null) 
			createRequests(network, accessRankings);
		
		if (requestsToMake.size() > 0) {
			Hashtable<Node, DataObject> ret = new Hashtable<Node, DataObject>();
			ret.putAll(requestsToMake);
			requestsToMake.clear();
			return ret;
		}
		return new Hashtable<Node, DataObject>();
	}
}